package iot.ht.controller.htuserlogin.service;

import iot.core.data.BaseDto;
import iot.core.data.PageData;
import iot.core.data.ServiceImpl;
import iot.core.data.entity.HTUser;
import iot.core.data.entity.HTUserDept;
import iot.core.data.entity.HTUserLogin;
import iot.core.data.entity.HTUserRole;
import iot.core.data.mapper.*;
import iot.core.exception.MyException;
import iot.core.manager.auth.AuthManager;
import iot.core.springboot.MyThreadLocal;
import iot.core.util.StreamUtil;
import iot.ht.controller.htuserlogin.data.HTUserLoginDebugDto;
import iot.ht.controller.htuserlogin.data.HTUserLoginQto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class HTUserLoginService extends ServiceImpl<HTUserLoginMapper, HTUserLogin> implements IHTUserLoginService {
    @Autowired
    HTDBMapper htdbMapper;


    @Override

    public PageData<HTUserLogin> Page(HTUserLoginQto qm){
        var qw=getBaseMapper().getQueryWrapper();
        if(StringUtils.hasText(qm.getState() )){
            var bIsOnline = qm.getState().equals("在线")  ;
            qw.eq("IsOnline",bIsOnline );
        }

        if(StringUtils.hasText(qm.getDTB() )){
            qw.ge("Crt_DT",qm.getDB() );
        }

        if(StringUtils.hasText(qm.getDTE() )){
            qw.lt("Crt_DT",qm.getDE() );
        }

        if(StringUtils.hasText(qm.getKey() )){
            qw.and(qr->qr.like("Name",qm.getKey())
                    .or()
                    .like("HTDept_Name",qm.getKey())
            );

        }
        var sort=qm.getSort();
        qw.orderBy(true,sort.isIsAsc(),sort.getFiled() );

        var r=getBaseMapper().page( qw,qm );
        return r;
    }

    @Override
    public int ForceLx(BaseDto mm)
    {
        var m=getById(mm.getId() );
        if (m.isIsOnline())
        {
            m.setIsOnline(false);
            MyThreadLocal.get().setLogMemo(String.format("强制%s 离线",m.getName()));
            return update(m);
        }
        return 0;
    }

    @Override
    public int SetDebug(HTUserLoginDebugDto mm)
    {
        var m=getById(mm.getId() );
        if (m.isIsOnline())
        {
            if (mm.isIsDebug()==mm.isIsDebug() )
            {
                m.setIsDebug(mm.isIsDebug() );
                MyThreadLocal.get().setLogMemo(String.format("设置%s 调试模式：%s",m.getName(),m.isIsDebug() ));
                return update(m);
            }
        }
        return 0;
    }

}
